package com.cac.demo.util.adapter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.util.Map;

/**
 * @className: JsonHelper
 * @description: （描述这个类的作用）
 * @author:
 * @date: 2024/6/22 11:25:25 周六
 * @version: 1.0
 */
public class JsonHelper {
    private static final Logger log = LoggerFactory.getLogger(JsonHelper.class);
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public JsonHelper() {
    }

    public static JsonHelper getInstance() {
        return new JsonHelper();
    }

    public <T> T readValue(String content, DateFormat dateFormat, TypeReference<T> valueTypeRef) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        try {
            mapper.setDateFormat(dateFormat);
            return mapper.readValue(content, valueTypeRef);
        } catch (Exception var6) {
            var6.printStackTrace();
            throw new RuntimeException(var6);
        }
    }

    public static ObjectMapper getObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return objectMapper;
    }

    public static <T> T readValue(String content, TypeReference<T> valueTypeRef) {
        try {
            return OBJECT_MAPPER.readValue(content, valueTypeRef);
        } catch (Exception var3) {
            log.error("读取json数据失败", var3);
            throw new RuntimeException(var3);
        }
    }

    public static <T> T readValue(String content, Class<T> clazz) {
        try {
            return OBJECT_MAPPER.readValue(content, clazz);
        } catch (Exception var3) {
            log.error("读取json数据失败", var3);
            throw new RuntimeException(var3);
        }
    }

    public static <T> T readValue(InputStream in, Class<T> clazz) {
        Object value;
        try {
            value = OBJECT_MAPPER.readValue(in, clazz);
        } catch (IOException var4) {
            value = null;
        }
        return (T) value;
    }

    public static String writeValueAsString(Object target) throws JsonProcessingException {
        return OBJECT_MAPPER.writeValueAsString(target);
    }

    public static <T> T to(Object form, Class<T> to) {
        try {
            return readValue(writeValueAsString(form), to);
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    public static <T> T to(Object form, TypeReference<T> to) {
        try {
            return readValue(writeValueAsString(form), to);
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    public static Map<String, Object> toMap(Object form) {
        try {
            return (Map)readValue(writeValueAsString(form), new TypeReference<Map<String, Object>>() {
            });
        } catch (Exception var2) {
            throw new RuntimeException(var2);
        }
    }

    public static <T> T fromMap(Map<String, Object> form, Class<T> type) {
        try {
            return readValue(writeValueAsString(form), type);
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    static {
        OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
}
